﻿using Abp.Dependency;
using Abp.Runtime.Session;
using System;
using System.Linq;
using System.Security.Claims;

namespace Hicap.QRCodeWarehouse.Core.Common
{
    /// <summary>
    /// Add CompanyId to session
    /// </summary>
    public static class SessionExtension
    {
        public static int? GetCompanyId(this IAbpSession abpSession)
        {
            ClaimsPrincipal principal = IocManager.Instance.Resolve<IPrincipalAccessor>().Principal;

            Claim claim = principal != null
                ? principal.Claims.FirstOrDefault(c => c.Type == ClaimTypes.GroupSid)
                : null;

            if (!string.IsNullOrEmpty(claim?.Value))
                return new int?(Convert.ToInt32(claim.Value));

            return new int?();
        }
    }
}